//
//  TestModel.swift
//  CooperSwift
//
//  Created by CooperSwift on 2021/04/27 12:06:20.
//  Copyright © 2021 CooperSwift. All rights reserved.
//

//import WCDBSwift

class TestModel: ColumnCodable, TableCodable {
    
    var a: Int?
    var b: String?
    var c: CModel?
//    var f: Array<FModel>?

    
    static var columnType: ColumnType {
        return .BLOB
    }
    
    required init?(with value: FundamentalValue) {
        let data = value.dataValue
        guard data.count > 0 else {
            return nil
        }
        guard let dictionary = try? JSONDecoder().decode(CModel.self, from: data) else {
            return nil
        }
    }
    
    func archivedValue() -> FundamentalValue {
        guard let data = try? JSONEncoder().encode(self) else {
                   return FundamentalValue(nil)
               }
               return FundamentalValue(data)
    }
    
    
    enum CodingKeys: String, CodingTableKey {
        typealias Root = TestModel

        case a
        case b
        case c
//        case f

        static let objectRelationalMapping = TableBinding(CodingKeys.self)

        //Column constraints for primary key, unique, not null, default value and so on. It is optional.
        //static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
        //    return [
        //        .variable: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true),
        //        .variable2: ColumnConstraintBinding(isUnique: true)
        //    ]
        //}

        //Index bindings. It is optional.
        //static var indexBindings: [IndexBinding.Subfix: IndexBinding]? {
        //    return [
        //        "_index": IndexBinding(indexesBy: CodingKeys.variable2)
        //    ]
        //}

        //Table constraints for multi-primary, multi-unique and so on. It is optional.
        //static var tableConstraintBindings: [TableConstraintBinding.Name: TableConstraintBinding]? {
        //    return [
        //        "MultiPrimaryConstraint": MultiPrimaryBinding(indexesBy: variable2.asIndex(orderBy: .descending), variable3.primaryKeyPart2)
        //    ]
        //}

        //Virtual table binding for FTS and so on. It is optional.
        //static var virtualTableBinding: VirtualTableBinding? {
        //    return VirtualTableBinding(with: .fts3, and: ModuleArgument(with: .WCDB))
        //}
    }

    //Properties below are needed only the primary key is auto-incremental
    //var isAutoIncrement: Bool = false
    //var lastInsertedRowID: Int64 = 0
}

    
